
// Copyright (C) 2011-2012 Luca Piccioni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;

namespace Derm.Render
{
	/// <summary>
	/// A collection of vertex array objects.
	/// </summary>
	public class VertexMatrixObject : RenderResource
	{
		#region Vertex Arrays

		/// <summary>
		/// Add a vertex array to this VertexMatrixObject.
		/// </summary>
		/// <param name="vertexArray"></param>
		public void AddVertexArray(VertexArrayObject vertexArray)
		{
			if (vertexArray == null)
				throw new ArgumentNullException("vertexArray");

			mVertexArrays.Add(vertexArray);
		}

		/// <summary>
		/// 
		/// </summary>
		public IEnumerable<VertexArrayObject> Arrays { get { return (mVertexArrays); } }

		/// <summary>
		/// Number of vertex arrays collected by this vertex matrix.
		/// </summary>
		public uint VertexArraysCount { get { return ((uint) mVertexArrays.Count); } }

		/// <summary>
		/// Render this vertex array.
		/// </summary>
		/// <param name="ctx">
		/// The <see cref="RenderContext"/> used for rendering.
		/// </param>
		/// <param name="shaderProgram">
		/// The <see cref="ShaderProgram"/> which requires vertex array bindings.
		/// </param>
		public void RenderArrays(RenderContext ctx, ShaderProgram shaderProgram)
		{
			foreach (VertexArrayObject vbo in mVertexArrays)
				vbo.DrawVertexArray(ctx, shaderProgram);
		}

		/// <summary>
		/// The vertex arrays compositing this VertexMatrixObject.
		/// </summary>
		private readonly List<VertexArrayObject> mVertexArrays = new List<VertexArrayObject>();

		#endregion

		#region RenderResource Overrides

		/// <summary>
		/// Vertex array object class.
		/// </summary>
		internal static readonly Guid VertexMatrixObjectClass = new Guid("7115D343-827C-4E39-81A2-18714BF37BF3");

		/// <summary>
		/// Vertex array object class.
		/// </summary>
		public override Guid ObjectClass { get { return (VertexMatrixObjectClass); } }

		/// <summary>
		/// Determine whether this object requires a name bound to a context or not.
		/// </summary>
		/// <param name="ctx">
		/// A <see cref="RenderContext"/> used for creating this object name.
		/// </param>
		/// <returns>
		/// It returns a false, since this is not a real OpenGL object.
		/// </returns>
		protected override bool RequiresName(RenderContext ctx)
		{
			return (false);
		}

		/// <summary>
		/// Actually create this RenderResource resources.
		/// </summary>
		/// <param name="ctx">
		/// A <see cref="RenderContext"/> used for allocating resources.
		/// </param>
		protected override void CreateObject(RenderContext ctx)
		{
			// Create vertex arrays
			foreach (VertexArrayObject vbo in mVertexArrays)
				vbo.Create(ctx);
		}

		#endregion
	}
}
